package priv.stones.array;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @Author: Stones
 * @Description:
 * @Date: 2017/9/25 13:44
 */
class Student implements Comparable<Student> {

    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Student o) {
         if(this.age>o.age){
             return 1;
         } else if (this.age == o.age) {
             return 0;
         } else {
             return -1;
         }
    }
}

class comparator_student implements Comparator<Student> {


    @Override
    public int compare(Student o1, Student o2) {
        if(o1.getAge()>o2.getAge()){
            return 1;
        } else if (o1.getAge() == o2.getAge()) {
            return 0;
        } else {
            return -1;
        }
    }


}


public class Comparable1 {

    public static void main(String[] args) {

        Student[] students = new Student[]{
                new Student("张三", 12),
                new Student("李四", 50),
                new Student("王五", 2),
                new Student("赵六", 20)
        };
//        Arrays.sort(students);

        System.out.println(Arrays.toString(students));
        Arrays.sort(students, new comparator_student());


        System.out.println(Arrays.toString(students));

//        Student student1 = new Student("张三", 144);
//        Student student2 = new Student("张三", 13);
//        System.out.println(student1.compareTo(student2));
    }

}
